package com.viphuli.app.tool.fragment;

import android.app.Activity;
import android.content.Intent;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.OnClick;

import com.loopj.android.http.RequestParams;
import com.offroader.utils.JsonUtils;
import com.offroader.utils.StringUtils;
import com.offroader.utils.ViewUtils;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.bean.part.CircleDepartment;
import com.viphuli.app.tool.bean.part.CircleHospital;
import com.viphuli.app.tool.bean.part.CircleInfo;
import com.viphuli.app.tool.common.AccessTokenKeeper;
import com.viphuli.app.tool.common.Constants;
import com.viphuli.app.tool.common.MyPageHelper;
import com.viphuli.app.tool.handler.MyBaseHttpResponseHandler.HandlerType;
import com.viphuli.app.tool.http.ApiRequest;

/**
 * 小组信息
 * 
 * @author li.li
 *
 */
public class CircleInfoFragment extends BaseProgressFragment {

	private CircleInfo circleInfo;

	@Bind(R.id.id_circle_name)
	protected TextView nameText;

	@Bind(R.id.id_circle_hos)
	protected TextView hosText;

	@Bind(R.id.id_circle_office)
	protected TextView departmentText;

	private CircleHospital circleHospital;

	private CircleDepartment circleDepartment;

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_circle_info;
	}

	@Override
	protected void initParams() {
	}

	@Override
	protected void initView() {

	}

	@Override
	protected void initData() {
		
		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

		if (tokenKeeper.isLogin()) {
			RequestParams params = new RequestParams();
			params.put("circle_id", tokenKeeper.getUser().circleId());
			ApiRequest.circleInfo.request(this, params);
		}

	}

	@OnClick(R.id.id_circle_two_dim_code_btn)
	public void goToCircleTwoDimCode() {

		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

		if (tokenKeeper.isLogin()) {
			CircleUserAddWechatFragment.go(getActivity(), tokenKeeper.getUser().circleId());
		}

	}

	@OnClick(R.id.id_circle_member_btn)
	public void goToCircleMember() {

		CircleMemberFragment.go(getActivity());
	}

	@OnClick(R.id.id_circle_exit_btn)
	public void exitCircle() {

		AccessTokenKeeper token = AccessTokenKeeper.readAccessToken(getActivity());

		if (token.isLogin()) {

			RequestParams params = new RequestParams();
			params.put("uid", token.getOpenId());
			params.put("circle_id", token.getUser().circleId());
			ApiRequest.circleExit.request(this, params);

		}

	}

	@OnClick(R.id.id_circle_name_item)
	public void clickCircleName() {
		String title = "小组名称";
		String content = nameText.getText().toString();
		String hospital = circleInfo.getHospital();
		String department = circleInfo.getDepartment();

		UpdateCircleNameFragment.go(getActivity(), title, content, hospital, department);

	}

	@OnClick(R.id.id_circle_hospital_item)
	public void clickHos() {

		//		String title = "医院";
		//		String content = hosText.getText().toString();
		//		String name = nameText.getText().toString();
		//		String department = departmentText.getText().toString();
		//
		//		UpdateCircleHostFragment.go(getActivity(), title, content, name, department);

		CircleHospitalListFragment.go(getActivity());

	}

	@OnClick(R.id.id_circle_department_item)
	public void clickDepartment() {

		//		String title = "科室";
		//		String content = departmentText.getText().toString();
		//		String name = nameText.getText().toString();
		//		String hospital = hosText.getText().toString();
		//
		//		UpdateCircleDepartmentFragment.go(getActivity(), title, content, name, hospital);

		if (circleHospital != null)
			CircleDepartmentListFragment.go(getActivity(), circleHospital.getHospitalId());
		else
			ViewUtils.toast("请选择医院");

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (resultCode == Activity.RESULT_OK) {

			if (requestCode == Constants.ACTIVITY_REQUEST_HOSPITAL_SELECT_CODE) {

				if (data != null) {

					String circleHospitalJson = data.getStringExtra("circle_hospital");

					if (StringUtils.isNotBlank(circleHospitalJson)) {
						circleHospital = JsonUtils.fromJson(circleHospitalJson, CircleHospital.class);
						hosText.setText(circleHospital.getName());
					}

				}

			} else if (requestCode == Constants.ACTIVITY_REQUEST_DEPARTMENT_SELECT_CODE) {

				if (data != null) {

					String circleDepartmentJson = data.getStringExtra("circle_department");

					if (StringUtils.isNotBlank(circleDepartmentJson)) {
						circleDepartment = JsonUtils.fromJson(circleDepartmentJson, CircleDepartment.class);
						departmentText.setText(circleDepartment.getName());
						
						//修改小组信息
						AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());
						if (tokenKeeper.isLogin()) {
							
							RequestParams params = new RequestParams();
							params.put("uid", tokenKeeper.getOpenId());
							params.put("name", nameText.getText().toString());
							params.put("circle_id", tokenKeeper.getUser().circleId());
							params.put("hospital", circleHospital.getName());
							params.put("department", circleDepartment.getName());
							params.put("hospital_id", circleHospital.getHospitalId());
							params.put("department_id", circleDepartment.getDepartmentId());

							ApiRequest.circleInfoChange.request(this, params, HandlerType.waitting);

						}
					}

				}

			}
		}

	}

	public void setCircleInfo(CircleInfo circleInfo) {
		this.circleInfo = circleInfo;
	}

	public TextView getNameText() {
		return nameText;
	}

	public TextView getHosText() {
		return hosText;
	}

	public TextView getDepartmentText() {
		return departmentText;
	}

	public static void go(Activity act) {
		MyPageHelper.circleInfo.showMyPage(act);
	}

}
